<%@ page contentType="text/html; charset=UTF-8"%>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk" />
<meta http-equiv="Content-Language" content="zh-CN" />
<meta name="roots" content="" />
<meta name="Keywords" content="" />
<meta name="Description" content="" />
<link href="css/map.css" type="text/css" rel="stylesheet" />
<link href="layer/css/demo.css" type="text/css" rel="stylesheet" />
<script src="layer/js/jquery.js" type="text/javascript"></script>
<script src="layer/js/PopupDiv_v1.0.js" type="text/javascript"></script>
<script src="layer/js/demo.js" type="text/javascript"></script>
<title></title>

<script>
var selectedMapId;
var baseMap = new BaseMap();
var dotMap = new BaseMap();
var mapMap = new BaseMap();
var clickMapId = 0;//被选中的mapId

initMapIds();

function initMapIds(){
	$.post("mapAction", {
		"actionType" : "initMapIds"
	}, function(data) {
		if(data.length < 1){
			return;
		}
		mapMap.clear();
		for(var i = 0 ; i < data.length; i++){
			mapMap.put(i, data[i]);
		}
		clickMapId = mapMap.get(0);
		init(clickMapId);
	}, "json");
}

	function init(mapId){
		selectedMapId = mapId;
		getMapKeys();
		initAllBaseTd();
		$.post("mapAction", {
			"actionType" : "initMap",
			"mapId": mapId
		}, function(data) {
			if(data == null){
				return;
			}
			var mapBean = data;//mapBean
			var mapId = mapBean.mapId;//mapId
			var kalMan = mapBean.kalMan;//是否高精度
			var allBase = mapBean.allBase;//地图上所有基站
			var areaList = mapBean.areaList;//地图上区域集合
			var netMap = mapBean.netMap;//地图上网络分组
			//卡尔曼滤波
			if(kalMan == true){
				$("input[name=isUsingkaerMan]:eq(0)").attr("checked",'checked');
			}else{
				$("input[name=isUsingkaerMan]:eq(1)").attr("checked",'checked');
			}
			
			//基站列表	
			var baseTd = "";
			for(var j = 0; j < allBase.length; j++){
				var baseStation = allBase[j];
				baseTd = baseTd + "<option value='"+baseStation.id+"'>" + baseStation.name + "</option>";
			}
			$("#baseBox").html(baseTd);
			
			//区域列表
			var areaTd = "";
			for(var j = 0; j < areaList.length; j++){
				var area = areaList[j];
				areaTd = areaTd + "<option>区域" + area.areaId + "</option>";
			}
			$("#areaBox").html(areaTd);
				
			//网络列表
			var netTd = "";
			for(var key in netMap){
				netTd = netTd + "<option>网络" + key + "</option>";
			}
			$("#netBox").html(netTd);
				
			changeBaseStation();
			changeArea();
			changeNet();
		}, "json");
	}
	
	function getMapKeys(){
		$.post("mapAction", {
			"actionType" : "initMapKeys"
		}, function(data) {
			var mapMenu = "";
			mapMap.clear();
			for(var i = 0 ; i < data.length; i++){
				mapMap.put(i, data[i]);
				if(data[i] == clickMapId){
					mapMenu = mapMenu + "<li id='menu" + data[i] + "' style='background:blue' onclick='changeMap(this.id)'>map" + data[i] + "</li>";
				}else{
					mapMenu = mapMenu + "<li id='menu" + data[i] + "' onclick='changeMap(this.id)'>map" + data[i] + "</li>";
				}
			}
			$("#mapUl").html(mapMenu);
		}, "json");
	}
	
	//切换地图
	function changeMap(li){
		var map = $("#"+li).html();
		var mapId = map.substr(3, map.length);
		clickMapId = mapId;//记录被选中的mapId
		init(mapId);
	}
	
	//初始化基站列表
	function initAllBaseTd(){
		$.post("mapAction", {
			"actionType" : "initAllBase",
			"mapId" : selectedMapId
		}, function(data) {
			var allBase = "";
			baseMap.clear();
			for(var i = 0; i < data.length; i++){
				baseMap.put(data[i].id, data[i].name);
				allBase = allBase + "<input type='checkbox' style='width: 20px' name='base_" + data[i].id + "' />"+data[i].name;
			}
			$("#allBaseTd").html(allBase);
		}, "json");
	}
	//编辑网络基站
	function editNetBase(){
		var ids = baseMap.keys();
		var selectedIds = "";
		for(var id = 0; id < ids.length; id++){
			if($("input[name='base_"+ ids[id] +"']").attr("checked") == true){
				selectedIds = selectedIds + ids[id] + ",";
			}
		}
		var net = $("#netBox").val();
		$.post("mapAction", {
			"actionType" : "editNetBase",
			"mapId" : selectedMapId,
			"netId" : net.substr(2, net.length),
			"basesIds" : selectedIds
		}, function(data) {
		}, "json");
		init(selectedMapId);
	}
	function updateNet(){
		var base = $("#netInfo").val();
		var net = $("#netBox").val();
		$.post("mapAction", {
			"actionType" : "updateBase",
			"mapId" : selectedMapId,
			"netId" : net.substr(2, net.length),
			"baseId" : base.substr(2, base.length),
			"shortAddress" : $("#shortAddress").val(),
			"gongLv" : $("#gongLv").val(),
			"channel" : $("#channel").val(),
			"f_start" : $("#f_start").val(),
			"f_end" : $("#f_end").val(),
			"f_step" : $("#f_step").val(),
			"distance_Offset" : $("#distanceOffset").val()
		}, function(data) {
			showInfo(data, "修改");
		}, "json");
		init(selectedMapId);
	}
	
	//切换基站
	function changeBaseStation(){
		var con = $("#baseBox").val();
		if(con == null){
			return;
		}
		var base = document.getElementById("baseBox");;
		var baseId = base.options[base.selectedIndex].value;
		$.post("mapAction", {
			"actionType" : "getBaseById",
			"mapId" : selectedMapId,
			"baseId" : baseId
		}, function(data) {
			$("#baseStationMac").val(data.mac);
			$("#baseStationX").val(data.x);
			$("#baseStationY").val(data.y);
			$("#baseStationZ").val(data.z);
			$("#tempNumber").val(data.baseStationTempNumber);
			$("#jn").val(data.baseStationJN);
			$("#fh").val(data.baseStationFH);
			if(data.useBaseStationFH == true){
				$("input[name='useFH']").attr("checked","checked");
			}else{
				$("input[name='useFH']").attr("checked","");
			}
		}, "json");
	}
	//切换区域
	function changeArea(){
		var area = $("#areaBox").val();
		if(area == null){
			return;
		}
		$.post("mapAction", {
			"actionType" : "getAreaById",
			"mapId" : selectedMapId,
			"areaId" : area.substr(2, area.length)
		}, function(data) {
			var dotList = data.dotList;
			var areaInfo = "";
			dotMap.clear();
			for(var i = 0; i < dotList.length; i++){
				var dot = dotList[i];
				dotMap.put(dot.id, dot);
				areaInfo = areaInfo + "x"+dot.id+":<input type='text' id='dot" + dot.id + "X' value='" + dot.x + "' />y" + dot.id + ":<input type='text' id='dot" + dot.id + "Y' value='" + dot.y + "' />";
			}	
			$("#areaInfoTd").html(areaInfo);
			$("#vedioId").val(data.vedioId);
		
		}, "json");
	}
	//切换网络
	function changeNet(){
		var net = $("#netBox").val();
		if(net == null){
			return;
		}
		$.post("mapAction", {
			"actionType" : "getNetById",
			"mapId" : selectedMapId,
			"netId" : net.substr(2, net.length)
		}, function(data) {
			var allBase = data.bases;
			var netInfo = "";
			for(var i = 0; i < allBase.length; i++){
				var base = allBase[i];
				netInfo = netInfo + "<option>"+base.name+"</option>";
			}
			$("#netInfo").html(netInfo);
			changeNetAndBase();
		}, "json");
	}
	function changeNetAndBase(){
		var net = $("#netBox").val();
		if(net == null){
			return;
		}
		var base = $("#netInfo").val();
		if(base == null){
			return;
		}
		$.post("mapAction", {
			"actionType" : "getBaseByNetIdAndBaseId",
			"mapId" : selectedMapId,
			"netId" : net.substr(2, net.length),
			"baseId" : base.substr(2, base.length)
		}, function(data) {
			$("#shortAddress").val(data.shortAddress);
			$("#gongLv").val(data.gongLv);
			$("#channel").val(data.channel);
			$("#f_start").val(data.f_start);
			$("#f_end").val(data.f_end);
			$("#f_step").val(data.f_step);
			$("#distanceOffset").val(data.distance_offset);
		}, "json");
	}
	//地图上增加基站
	function addBaseStation(){
		$.post("mapAction", {
			"actionType" : "addBase",
			"mapId" : selectedMapId,
//			"name" : $("#baseName").val(),
			"name" : "",
			"x" : $("#baseX").val(),
			"y" : $("#baseY").val()
		}, function(data) {
			showInfo(data, "添加");
			init(selectedMapId);
		}, "json");
	} 
	//地图上删除基站
	function deleteBaseStation(){
		var base = document.getElementById("baseBox");
		var baseId = base.options[base.selectedIndex].value;
		$.post("mapAction", {
			"actionType" : "deleteBase",
			"mapId" : selectedMapId,
			"baseId" : baseId
		}, function(data) {
		}, "json");
		init(selectedMapId);
	}
	//增加区域
	function addArea(){
		$.post("mapAction", {
			"actionType" : "addArea",
			"mapId" : selectedMapId
		}, function(data) {
			showInfo(data, "添加");
			init(selectedMapId);
		}, "json");
	}
	//删除区域
	function deleteArea(){
		var area = $("#areaBox").val();
		$.post("mapAction", {
			"actionType" : "deleteArea",
			"mapId" : selectedMapId,
			"areaId" : area.substr(2, area.length)
		}, function(data) {
		}, "json");
		init(selectedMapId);
	}
	//增加区域顶点
	function addAreaDot(){
		var area = $("#areaBox").val();
		$.post("mapAction", {
			"actionType" : "addAreaDot",
			"mapId" : selectedMapId,
			"areaId" : area.substr(2, area.length),
			"x" : $("#dotX").val(),
			"y" : $("#dotY").val()
		}, function(data) {
		}, "json");
		init(selectedMapId);
	}
	//增加网络
	function addNet(){
		$.post("mapAction", {
			"actionType" : "addNet",
			"mapId" : selectedMapId
		}, function(data) {
			showInfo(data, "添加");
			init(selectedMapId);
		}, "json");
	}
	//删除网络
	function deleteNet(){
		var net = $("#netBox").val();
		$.post("mapAction", {
			"actionType" : "deleteNet",
			"mapId" : selectedMapId,
			"netId" : net.substr(2, net.length)
		}, function(data) {
		});
		init(selectedMapId);
	}
	
	 function BaseMap() {     
	        this.elements = new Array();     
	           
	        //删除MAP所有元素     
	        this.clear = function() {     
	            this.elements = new Array();     
	        }; 
	        
	        //判断MAP是否为空     
	        this.isEmpty = function() {     
	            return(this.elements.length < 1);     
	        };
	           
	        //向MAP中增加元素（key, value)      
	        this.put = function(_key, _value) {     
	            this.elements.push( {     
	                key : _key,     
	                value : _value     
	            });     
	        };
	           
	        //获取MAP中所有KEY的数组（ARRAY）     
	        this.keys = function() {     
	            var arr = new Array();     
	            for(i = 0; i < this.elements.length; i++) {     
	                arr.push(this.elements[i].key);     
	            }     
	            return arr;     
	        };
	        
	      //获取指定KEY的元素值VALUE，失败返回NULL     
	        this.get = function(_key) {     
	            try{     
	                for(i = 0; i < this.elements.length; i++) {     
	                    if(this.elements[i].key == _key) {     
	                        return this.elements[i].value;     
	                    }     
	                }     
	            } catch(e) {     
	                return null;     
	            }     
	        }   ;
	    }
	 //是否使用卡尔曼滤波
	 function dingWeiModel(){
		 $.post("mapAction", {
				"actionType" : "useKaerMan",
				"mapId" : selectedMapId,
				"selectkaerMan" : $("#useKaerMan").attr("checked")
			}, function(data) {
				showInfo(data, "修改");
			}, "json");
	 }
	 //增加地图
	 function addMap(){
		 $.post("mapAction", {
				"actionType" : "addMap"
			}, function(data) {
			});
		 getMapKeys();
	 }	
	 //删除地图
	 function deleteMap(){
		 $.post("mapAction", {
				"actionType" : "deleteMap",
				"mapId" : selectedMapId
			}, function(data) {
			});
		 	initMapIds();
	 }
	 //修改基站信息
	 function updateBaseStation(){
		 var con = $("#baseBox").val();
			if(con == null){
				return;
			}
			var base = document.getElementById("baseBox");;
			var baseId = base.options[base.selectedIndex].value;
			var x = $("#baseStationX").val();
			var y = $("#baseStationY").val();
			var z = $("#baseStationZ").val();
			var temp = $("#tempNumber").val();
			var jn = $("#jn").val();
			var fh = $("#fh").val();
			var check = $("input[name='useFH']").attr("checked")
			var mac = $("#baseStationMac").val();
			$.post("mapAction", {
				"actionType" : "updateBaseStation",
				"mapId" : selectedMapId,
				"baseId" : baseId,
				"baseInfo" : x+","+y+","+z+","+temp+","+jn+","+fh+","+check+","+mac
			}, function(data) {
				showInfo(data, "修改");
		 		init(selectedMapId);
			}, "json");
	 }
	 //修改区域顶点坐标
	 function updateAreaDot(){
		 var ids = dotMap.keys();
		 if(ids.length <= 0){
			 return;
		 }
		 var area = $("#areaBox").val();
		 if(area == null){
			return;
		 }
		 var dotInfo = "";
		 for(var i = 0; i < ids.length; i++){
			 dotInfo = dotInfo + ids[i] + "," + $("#dot"+ids[i]+"X").val() + "," + $("#dot"+ids[i]+"Y").val() + ";";
		 }
		 $.post("mapAction", {
				"actionType" : "updateAreaDot",
				"mapId" : selectedMapId,
				"areaId" : area.substr(2, area.length),
				"dotInfo" : dotInfo,
				"vedioId" : $("#vedioId").val()
			}, function(data) {
					showInfo(data, "修改");
			}, "json");
			init(selectedMapId);		 
	 }
	 function showInfo(data, info){
		 if (data.success == "true") {
				alert(info + "成功");
			} else {
				alert(info + "失败");
			}
	 }
	 function changeSubmitUrl(){
		 document.form1.action = "upload?mapId=" + selectedMapId;//根据地图ID上传地图
		 document.form1.submit();
	 }
</script>
</head>
<body>
	地图配置：
	<div id="Tab">
		<div class="Menubox" id="menu">
			<ul id="mapUl"></ul>
			<button onclick="addMap()" style="float: right; margin-top: 5px">添加地图</button>
			<button onclick="deleteMap()" style="float: right; margin-top: 5px">删除地图</button>
		</div>
		<div class="Contentbox" id="content">
			<table border="1" cellpadding="2" cellspacing="5">
				<tr>
					<td>定位模式</td>
					<td colspan="2">
						<input type="radio" id="useKaerMan" value="0" name="isUsingkaerMan" />高精度模式
						<input type="radio" id="notUseKaerMan" value="1" name="isUsingkaerMan" />普通模式
					</td>
					<td><button onclick="dingWeiModel()">确定</button></td>
				</tr>
				
				<form method="post" action="upload?id=1" enctype="multipart/form-data" name="form1">
				<tr>
					<td>设置地图背景</td>
					<td colspan="2">
						<label>地图：</label>
						<input type="file" name="file" style="width: 220px; height: 22px" />
					</td>
					<!--  <td><input type="submit"  value="提交" style="width: 45px;" /></td>-->
					<td><button onclick="changeSubmitUrl()">提交</button></td>
				</tr>
				</form>
				
				<tr>
					<td>基站列表</td>
					<td id="baseBoxTd"><select id="baseBox" style="width: 65px" onchange="changeBaseStation()"></select></td>
					<td id="baseInfoTd">
						mac:<input type="text" id="baseStationMac" />
						x:<input type="text" id="baseStationX" />
						y:<input type="text" id="baseStationY" />
						z:<input type="text" id="baseStationZ" />
						误差参数:<input type="text" id="tempNumber" />厘米
						跳动界限:<input type="text" id="jn" />厘米
						高度修正:<input type="text" id="fh" />厘米
						<input type="checkbox" name="useFH" style="width: 10px;" />启用修正
					</td>
					<td>
						<button onclick="updateBaseStation()">确定修改</button>
						<button id="addBaseStation">添加基站</button>
						<button id="deleteBaseStation" onclick="deleteBaseStation()">删除基站</button>
					</td>
				</tr>
				
				<tr>
					<td>区域列表</td>
					<td id="areaBoxTd"><select id="areaBox" style="width: 65px" onchange="changeArea()"></select></td>
					<td>CAM编号：<input type="text" id="vedioId" /><div id="areaInfoTd"></div></td>
					<td>
						<button onclick="updateAreaDot()">确定修改</button>
						<button id="addArea" onclick="addArea()" >添加区域</button>
						<button id="deleteArea" onclick="deleteArea()">删除区域</button>
						<button id="addDot">增加顶点</button>
					</td>
				</tr>
				
				<tr>
					<td>网络分组</td>
					<td id="netBoxTd">
						<select id="netBox" style="width: 65px" onchange="changeNet()"></select>
					</td>
					<td>
						<div>
							短地址:<input type="text" id="shortAddress" />
							发射功率:<input type="text" id="gongLv" />
							通讯信道:<input type="text" id="channel" />
							测距F_start:<input type="text" id="f_start" />
							测距F_end:<input type="text" id="f_end" />
							测距F_step:<input type="text" id="f_step" />
							测距distanceOffset:<input type="text" id="distanceOffset" />
						</div>
						<div>包含基站：<select id="netInfo" style="width: 65px" onchange="changeNetAndBase()">
							
							</select></div>
					</td>
					<td>
						<button id="addNet" onclick="updateNet()">确定修改</button>
						<button id="addNet" onclick="addNet()">添加网络</button>
						<button id="deletNet" onclick="deleteNet()">删除网络</button>
						<button id="addNetBase">编辑基站</button>
					</td>
				</tr>
			</table>
		</div>
	</div>
	
	
	
	<div id="addBaseStationDiv" style="display:none" align="left"><br/>
	<table style="border: 1px solid #e4e4e4;">
		<!-- <tr>
			<td>基站名字:</td>
			<td><input id="baseName" type="text" value="" /></td>
		</tr>
		 -->
		<tr>
			<td>x坐标:</td>
			<td><input id="baseX" type="text" value="" /></td>
		</tr>	
		<tr>
			<td>y坐标:</td>
			<td><input id="baseY" type="text" value="" /></td>
		</tr>				
		<tr>
			<td></td>
			<td style="text-align: right;"><button id="save" onclick="addBaseStation()">确定</button></td>
		</tr>
	</table>
</div>  	


<div id="addDotDiv" style="display:none" align="left"><br/>
	<table style="border: 1px solid #e4e4e4;">
		<tr>
			<td>x坐标:</td>
			<td><input id="dotX" type="text" value="" /></td>
		</tr>	
		<tr>
			<td>y坐标:</td>
			<td><input id="dotY" type="text" value="" /></td>
		</tr>				
		<tr>
			<td></td>
			<td style="text-align: right;"><button id="save" onclick="addAreaDot()">确定</button></td>
		</tr>
	</table>
</div>


<div id="addNetBaseDiv" style="display:none" align="left"><br/>
	<table style="border: 1px solid #e4e4e4;">
		<tr>
			<td id="allBaseTd"></td>
		</tr>
		<tr>
			<td style="text-align: right;"><button id="save" onclick="editNetBase()">确定</button></td>
		</tr>
	</table>
</div>

</body>

